package thread;

import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class ThreadPoolDemo {
    public static void main(String[] args) {
        ExecutorService threadPool = Executors.newFixedThreadPool(2);

        //2指派任务
        for (int i = 0; i < 5; i++) {
            Runnable runnable = new Runnable() {
                @Override
                public void run() {
                    Thread thread = Thread.currentThread();
                    System.out.println(thread.getName() + "正在执行任务......");

                    try {
                        Thread.sleep(5000);
                    } catch (InterruptedException e) {
                    }
                    System.out.println(thread.getName() + "任务执行完毕........");
                    System.out.println();
                }
            };

            threadPool.execute(runnable);
            System.out.println("指派了一个线程任务");
        }
        threadPool.shutdown();
        System.out.println("线程池关闭了");
    }
}
